
<template>
  <div>
    <!-- 添加组件 -->
    <div>
      <el-dialog :title="`${data ? '编辑' : '新增'}`+'目录'" :visible="showDialog" class="addtafs" @close="closseDialog" :close-on-click-modal="false">
        <el-form :model="formData" class="addform" :rules="rules" ref="tagsFormRef">
          <el-form-item label="所属学科" prop="region" v-if="!$route.query.id">
            <el-select v-model="formData.region" placeholder="请选择" style="width:70%">
              <el-option :label="item.label" :value="item.value" v-for="item in subjectFrom" :key="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标签名称" prop="tagName" >
            <el-input v-model="formData.tagName" autocomplete="off" style="width:70%" placeholder="请输入标签名称"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closseDialog">取 消</el-button>
          <el-button type="primary" @click="submitAdd "
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
import { simple } from '../../api/hmmm/subjects'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: undefined
    }
  },
  data () {
    const data = this.data || {}
    return {
      // 修改的时候根据data初始化内容
      formData: {
        region: '',
        tagName: data.tagName || ''
      },
      // 学科简单列表
      subjectFrom: [],
      // 校验规则
      rules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: ['blur', 'change'] },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: ['blur', 'change'] }
        ],
        region: [
          { required: true, message: '请选择学科', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  mounted () {
    // 加载下拉框数据
    this.subjectlist()
    // console.log(this.$route.query)
  },
  methods: {

    // 在表格中添加数据
    submitAdd () {
      this.$refs.tagsFormRef.validate(async flag => {
        if (!flag) return
        const { data } = this
        if (data) {
          // 调用编辑接口
          await update({
            id: data.id,
            subjectID: this.formData.region,
            tagName: this.formData.tagName
          })
          this.$message.success('编辑成功')
        } else {
          // 调用新增接口
          await add({
            subjectID: this.$route.query.id ? Number(this.$route.query.id) : this.formData.region,
            tagName: this.formData.tagName
          })
          this.$message.success('添加成功')
        }
        this.closseDialog()
        // 通知到父组件更新数据
        this.$route.query ? this.$parent.pagesgoto() : this.$parent.lister()
      })
    },
    // 获取学科简单列表
    async subjectlist () {
      const { data } = await simple()
      this.subjectFrom = data
      if (this.data) {
        // 设置下拉框选中值
        this.formData.region = this.data.subjectID || ''
        console.log('asd', this.formData.region)
      }
    },
    // 点击关闭弹框
    closseDialog () {
      this.$emit('update:showDialog', false)
      this.$refs.tagsFormRef.resetFields()
    }
  }
}
</script>

<style scoped lang='scss'>
.addtafs{
  width: 850px;
  margin: 0 auto;
}
.addform{
  margin: 0 auto;
}
.dialog-footer{
  padding-left: 210px;
}
</style>
